﻿package mortal.game.manager
{
    import __AS3__.vec.*;
    import com.gengine.global.*;
    import com.mui.core.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.ui.*;
    import mortal.game.resource.*;

    public class CursorManager extends Object
    {
        public static const FIX:String = ImagesConst.Fix;
        public static const BUY:String = ImagesConst.Buy;
        public static const SELL:String = ImagesConst.Sell;
        public static const SAVE:String = ImagesConst.Save;
        public static const GET:String = ImagesConst.Get;
        public static const DELETE:String = ImagesConst.Delete;
        public static const UNMOVE:String = ImagesConst.UnMove;
        public static const FIGHT:String = ImagesConst.Fight;
        public static const COLLET:String = ImagesConst.Collect;
        public static const COLLETSAVE:String = ImagesConst.MouseSave1;
        public static const PICK:String = ImagesConst.Pick;
        public static const UNPICK:String = ImagesConst.UnPick;
        public static const DROP:String = ImagesConst.GetDrop1;
        public static const NPCTalk:String = ImagesConst.NPCTalk;
        public static const DEFENSE_COPY_SELL:String = "defense_copy_sell";
        public static const Arrow:String = "arrow";
        public static const BUTTON:String = "button";
        private static var _currentCurSorType:String = "arrow";
        private static var _isMouseInStage:Boolean = true;

        public function CursorManager()
        {
            return;
        }// end function

        public static function init() : void
        {
            registMouseCursor([ImagesConst.MouseNormal1, ImagesConst.MouseNormal2, ImagesConst.MouseNormal3, ImagesConst.MouseNormal4, ImagesConst.MouseNormal5], CursorManager.Arrow, 0.1, new Point(3, 2));
            registMouseCursor([ImagesConst.MouseClick1, ImagesConst.MouseClick2], CursorManager.BUTTON, 4, new Point(4, 2));
            registMouseCursor([ImagesConst.Sell], CursorManager.DEFENSE_COPY_SELL);
            registMouseCursor([ImagesConst.Fix], CursorManager.FIX);
            registMouseCursor([ImagesConst.Buy], CursorManager.BUY);
            registMouseCursor([ImagesConst.Sell], CursorManager.SELL);
            registMouseCursor([ImagesConst.Save], CursorManager.SAVE);
            registMouseCursor([ImagesConst.Get], CursorManager.GET);
            registMouseCursor([ImagesConst.Delete], CursorManager.DELETE);
            registMouseCursor([ImagesConst.UnMove], CursorManager.UNMOVE);
            registMouseCursor([ImagesConst.Fight], CursorManager.FIGHT);
            registMouseCursor([ImagesConst.Collect], CursorManager.COLLET);
            registMouseCursor([ImagesConst.Pick], CursorManager.PICK);
            registMouseCursor([ImagesConst.UnPick], CursorManager.UNPICK);
            registMouseCursor([ImagesConst.GetDrop1, ImagesConst.GetDrop2], CursorManager.DROP);
            registMouseCursor([ImagesConst.NPCTalk], CursorManager.NPCTalk);
            registMouseCursor([ImagesConst.MouseSave1, ImagesConst.MouseSave2, ImagesConst.MouseSave3], CursorManager.COLLETSAVE);
            CursorManager.showCursor(CursorManager.Arrow);
            Global.stage.addEventListener("mouseLeave", onMouseOut);
            return;
        }// end function

        private static function onMouseOut(event:Event) : void
        {
            _isMouseInStage = false;
            CursorManager.hideCursor();
            addInEvent();
            return;
        }// end function

        private static function addInEvent() : void
        {
            Global.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseIn);
            return;
        }// end function

        static function onMouseIn(event:Event) : void
        {
            _isMouseInStage = true;
            Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseIn);
            return;
        }// end function

        private static function registMouseCursor(param1:Array, param2:String, param3:Number = 4, param4:Point = null) : void
        {
            var _loc_5:* = new MouseCursorData();
            var _loc_6:* = new Vector.<BitmapData>;
            var _loc_7:int = 0;
            while (_loc_7 < param1.length)
            {
                
                _loc_6.push(GlobalClass.getBitmapData(param1[_loc_7]));
                _loc_7++;
            }
            _loc_5.data = _loc_6;
            if (param1.length == 0)
            {
                _loc_5.frameRate = 0;
            }
            else
            {
                _loc_5.frameRate = param3;
            }
            if (param4)
            {
                _loc_5.hotSpot = param4;
            }
            else
            {
                _loc_5.hotSpot = new Point(0, 0);
            }
            Mouse.registerCursor(param2, _loc_5);
            return;
        }// end function

        public static function register(param1:InteractiveObject) : void
        {
            param1.addEventListener(MouseEvent.ROLL_OVER, mouse_over);
            param1.addEventListener(MouseEvent.ROLL_OUT, mouse_out);
            return;
        }// end function

        public static function unRegister(param1:InteractiveObject) : void
        {
            param1.removeEventListener(MouseEvent.ROLL_OVER, mouse_over);
            param1.addEventListener(MouseEvent.ROLL_OUT, mouse_out);
            CursorManager.showCursor(CursorManager.Arrow);
            return;
        }// end function

        private static function mouse_over(event:MouseEvent) : void
        {
            switch(CursorManager.currentCurSorType)
            {
                case CursorManager.SELL:
                {
                    CursorManager.showCursor(CursorManager.SELL);
                    break;
                }
                case CursorManager.FIX:
                {
                    CursorManager.showCursor(CursorManager.FIX);
                    break;
                }
                case CursorManager.BUY:
                {
                    CursorManager.showCursor(CursorManager.BUY);
                    break;
                }
                case CursorManager.Arrow:
                {
                    CursorManager.showCursor(CursorManager.Arrow);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private static function mouse_out(event:MouseEvent) : void
        {
            CursorManager.showCursor(CursorManager.Arrow);
            return;
        }// end function

        public static function showCursor(param1:String = "") : void
        {
            if (!_isMouseInStage)
            {
                return;
            }
            if (param1 == CursorManagerImpl.cursorStyle)
            {
                if (!isSceneStyle())
                {
                    hideCursor();
                }
                return;
            }
            else
            {
                Mouse.cursor = param1;
            }
            return;
        }// end function

        public static function get isMouseInStage() : Boolean
        {
            return _isMouseInStage;
        }// end function

        public static function hideCursor() : void
        {
            CursorManager.showCursor(CursorManager.Arrow);
            return;
        }// end function

        public static function isCursorStyle(param1:String) : Boolean
        {
            return param1 == CursorManagerImpl.cursorStyle;
        }// end function

        public static function isSceneStyle() : Boolean
        {
            var _loc_1:* = Mouse.cursor;
            return _loc_1 == CursorManager.UNMOVE || _loc_1 == CursorManager.FIGHT || _loc_1 == CursorManager.COLLET || _loc_1 == CursorManager.COLLETSAVE || _loc_1 == CursorManager.PICK || _loc_1 == CursorManager.UNPICK || _loc_1 == CursorManager.DROP || _loc_1 == CursorManager.NPCTalk;
        }// end function

        public static function isInSpecialState() : Boolean
        {
            return _currentCurSorType == CursorManager.SELL || _currentCurSorType == CursorManager.FIX || _currentCurSorType == CursorManager.SAVE || _currentCurSorType == CursorManager.BUY || _currentCurSorType == CursorManager.GET;
        }// end function

        public static function hideFromButton() : void
        {
            if (Mouse.cursor == MouseCursor.BUTTON)
            {
                CursorManager.hideCursor();
            }
            return;
        }// end function

        public static function hideFromScene() : void
        {
            if (CursorManager.isSceneStyle())
            {
                CursorManager.hideCursor();
            }
            return;
        }// end function

        public static function get currentCurSorType() : String
        {
            return _currentCurSorType;
        }// end function

        public static function set currentCurSorType(param1:String) : void
        {
            if (_currentCurSorType == param1)
            {
                _currentCurSorType = Arrow;
            }
            else
            {
                _currentCurSorType = param1;
            }
            return;
        }// end function

    }
}
